package com.lb.jwitter.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.service.TblCTDTMonHocService;
import com.lb.jwitter.service.TblCanBoService;
import com.lb.jwitter.service.TblDKMHDotDKService;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.service.TblThongTinNhomLopMonHoc;
import com.lb.jwitter.service.TblThongTinNhomLopMonHocService;
import com.lb.jwitter.service.TblThongTinTKBService;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.lb.jwitter.util.Message;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

/**
 * @author QUOCVIET
 * 
 */

@ContextConfiguration(locations = "classpath:beans.xml")
public class TblThongTinNhomLopMonHocUpdateAction {
	private int pageSize;

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	private int page;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	private int pages;

	public int getPages() {
		return pages;
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	private String dataString;

	public String getDataString() {
		return dataString;
	}

	private Message msg;

	public Message getMsg() {
		return msg;
	}

	private int id;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	private String ten;

	public void setTen(String ten) {
		this.ten = ten;
	}

	public String getTen() {
		return ten;
	}

	private String ma;

	public void setMa(String ma) {
		this.ma = ma;
	}

	public String getMa() {
		return ma;
	}

	private int dotDKId;

	public void setDotDKId(int dotDKId) {
		this.dotDKId = dotDKId;
	}

	public int getDotDKId() {
		return dotDKId;
	}

	private TblDKMHDotDKService tblDKMHDotDKService;

	private int monHocId;

	public void setMonHocId(int monHocId) {
		this.monHocId = monHocId;
	}

	public int getMonHocId() {
		return monHocId;
	}

	private TblCTDTMonHocService tblCTDTMonHocService;

	private int tKBId;

	public void setTKBId(int tKBId) {
		this.tKBId = tKBId;
	}

	public int getTKBId() {
		return tKBId;
	}

	private TblThongTinTKBService tblThongTinTKBService;

	private int canBoId;

	public void setCanBoId(int canBoId) {
		this.canBoId = canBoId;
	}

	public int getCanBoId() {
		return canBoId;
	}

	private TblCanBoService tblCanBoService;

	private String ngayCapNhat;

	public void setNgayCapNhat(String ngayCapNhat) {
		this.ngayCapNhat = ngayCapNhat;
	}

	public String getNgayCapNhat() {
		return ngayCapNhat;
	}

	private TblThongTinNhomLopMonHoc tblThongTinNhomLopMonHoc;

	public void setTblThongTinNhomLopMonHoc(
			TblThongTinNhomLopMonHoc tblThongTinNhomLopMonHoc) {
		this.tblThongTinNhomLopMonHoc = tblThongTinNhomLopMonHoc;
	}

	private TblThongTinNhomLopMonHocService tblThongTinNhomLopMonHocService;

	private int preUpdate(Message msg, TblThongTinNhomLopMonHoc obj) {
		msg.setPreMessage_b(true);
		msg.setPreMessage_s("success");
		return 1;
	}

	private int postUpdate(Message msg, TblThongTinNhomLopMonHoc obj) {
		msg.setPostMessage_b(true);
		msg.setPostMessage_s("success");
		return 1;
	}

	private TblTaiKhoan taiKhoanDangNhap;

	public TblTaiKhoan getTaiKhoanDangNhap() {
		return taiKhoanDangNhap;
	}

	public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) {
		this.taiKhoanDangNhap = taiKhoanDangNhap;
	}

	private TblQuyenChiTietService tblQuyenChiTietService;
	@Autowired
	// Injected by Spring
	ApplicationContextProvider contextProvider;

	public String execute() throws Exception {
		try {
			Map session = ActionContext.getContext().getSession();
			taiKhoanDangNhap = (TblTaiKhoan) session.get("taiKhoan");
			if (taiKhoanDangNhap == null)
				return "noprivilege";
			ApplicationContext context = ApplicationContextProvider
					.getApplicationContext();
			tblQuyenChiTietService = (TblQuyenChiTietService) context
					.getBean("tbl_Quyen_ChiTietClient");
			if (!tblQuyenChiTietService.checkRight(taiKhoanDangNhap
					.getMaNhomQuyen().getId(), "tbl_ThongTin_NhomLop_MonHoc",
					"sua")) {
				return "noprivilege";
			}
			tblThongTinNhomLopMonHocService = (TblThongTinNhomLopMonHocService) context
					.getBean("tbl_ThongTin_NhomLop_MonHocClient");
			tblDKMHDotDKService = (TblDKMHDotDKService) context
					.getBean("tbl_DKMH_DotDKClient");

			tblCTDTMonHocService = (TblCTDTMonHocService) context
					.getBean("tbl_CTDT_MonHocClient");

			tblThongTinTKBService = (TblThongTinTKBService) context
					.getBean("tbl_ThongTin_TKBClient");

			tblCanBoService = (TblCanBoService) context
					.getBean("tbl_CanBoClient");

			msg = new Message();
			tblThongTinNhomLopMonHoc = tblThongTinNhomLopMonHocService
					.gettblThongTinNhomLopMonHoc(new Long(id));
			tblThongTinNhomLopMonHoc.setTen(ten);
			tblThongTinNhomLopMonHoc.setMa(ma);

			// tblThongTinNhomLopMonHoc.setDotDKId(tblDKMHDotDKService.gettblDKMHDotDK(new
			// Long(dotDKId)));
			// tblThongTinNhomLopMonHoc.setMonHocId(tblCTDTMonHocService.gettblCTDTMonHoc(new
			// Long(monHocId)));
			// tblThongTinNhomLopMonHoc.setTKBId(tblThongTinTKBService.gettblThongTinTKB(new
			// Long(tKBId)));

			tblThongTinNhomLopMonHoc.setCanBoId(taiKhoanDangNhap.getCanBoId());
			DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
			Date dd = new Date();
			GregorianCalendar c = new GregorianCalendar();
			c.setTime(dd);
			XMLGregorianCalendar d = DatatypeFactory.newInstance()
					.newXMLGregorianCalendar(c);
			tblThongTinNhomLopMonHoc.setNgayCapNhat(d);
			preUpdate(msg, tblThongTinNhomLopMonHoc);
			if (msg.isPreMessage_b()) {
				tblThongTinNhomLopMonHocService
						.updatetblThongTinNhomLopMonHoc(tblThongTinNhomLopMonHoc);
				postUpdate(msg, tblThongTinNhomLopMonHoc);
				if (msg.isPostMessage_b() == false) {
					return Action.ERROR;
				}
			} else {
				return Action.ERROR;
			}

			return Action.SUCCESS;
		} catch (Exception e) {
			System.out.println("Loi Update tbl_ThongTin_NhomLop_MonHoc Action");
			e.printStackTrace();
			return Action.ERROR;
		}
	}
}
